﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using bdnt_pets_system.DataServiceReference;
using bdnt_pets_system_Entity;

namespace bdnt_pets_system.PageViews.TeamManager
{
    public partial class TeamManagerControl : UserControl
    {
        DataServiceReference.DataServiceClient datasc;
        public TeamManagerControl()
        {
            InitializeComponent();
            datasc = new DataServiceReference.DataServiceClient();
            InitEvnet();
            QueryData();
        }

        void QueryData() {
            datasc.SelectTeamAsync(tbMc.Text);
        }

        void InitEvnet() {
            datasc.SelectTeamCompleted += new EventHandler<SelectTeamCompletedEventArgs>(datasc_SelectTeamCompleted);
            datasc.DeleteTeamCompleted += new EventHandler<DeleteTeamCompletedEventArgs>(datasc_DeleteTeamCompleted);
            btnCs.Click += new RoutedEventHandler(btnCs_Click);
            btnSs.Click += new RoutedEventHandler(btnSs_Click);
            btnNewObj.Click += new RoutedEventHandler(btnNewObj_Click);
        }

        void datasc_DeleteTeamCompleted(object sender, DeleteTeamCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("删除成功！", "系统提示", MessageBoxButton.OK);
                QueryData();
            }
            else
            {
                MessageBox.Show("删除失败！", "系统提示", MessageBoxButton.OK);
                return;
            }
        }

        void btnNewObj_Click(object sender, RoutedEventArgs e)
        {
            var newtt = new NewTeamControl ();
            App.ToPage(newtt);
        }

        void btnSs_Click(object sender, RoutedEventArgs e)
        {
            QueryData();
        }

        void btnCs_Click(object sender, RoutedEventArgs e)
        {
            tbMc.Text = "";
        }

        void datasc_SelectTeamCompleted(object sender, SelectTeamCompletedEventArgs e)
        {
            dgList.ItemsSource = e.Result;
        }

        private void hbUpdate_Click(object sender, RoutedEventArgs e)
        {
            var tId = (sender as HyperlinkButton).Tag as Tbl_Team;
            var newtt = new NewTeamControl(tId);
            App.ToPage(newtt);
        }

        private void hbDelete_Click(object sender, RoutedEventArgs e)
        {
            string tId = (sender as HyperlinkButton).Tag.ToString();
            if (tId == null)
            {
                return;
            }
            MessageBoxResult result = MessageBox.Show("确定要删除吗？", "系统提示", MessageBoxButton.OKCancel);
            if (MessageBoxResult.OK == result)
            {
                datasc.DeleteTeamAsync(Int32.Parse(tId));
            }
        }

    }
}
